#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct node
{
int value;
struct node *next;
};
struct node *create(struct node *list, int x)/*this function is to create the list*/
{  
   struct node *new_node;
 new_node=malloc(sizeof(struct node));
 new_node->value=x;
 new_node->next=list;
 return new_node;
}

 struct node *reverse(struct node*list)/*reversing the list*/
{

struct node *previous=NULL, *current ,*rev=NULL;
previous=malloc(sizeof(struct node));
current=malloc(sizeof(struct node));
current->value=list->value;
current->next=NULL;
previous=current;
list=list->next;
while(list!=NULL)
{
rev=malloc(sizeof(struct node));
rev->value=list->value;
rev->next=previous;
previous=rev;
list=list->next;

}
return rev;

}
void index1(struct node*list)/*giving the position of the given element*/
{ int a,b=0;
   printf("enter a number: \n");
   scanf("%d",&a);
     while((list->next)!=NULL)
      {       
       if(a==(list->value))
        {printf("index value of first occurence of %d is %d \n",a,b);
          break;}
      list=list->next;
       b++;
 }
}
void count(struct node *list)/*counting the number of occurence of the given element*/ 
{ int count=0,a;
 printf("enter the number you want to count:\n");
 scanf("%d",&a);
 while(list!=NULL)
 {if(a==list->value) 
  {count++;}
  list=list->next; 
 }
 printf("total number of occurences of %d is %d \n",a,count);
}
struct node *delete(struct node *list)/*deleting the given node*/
{int a;
 struct node *current,*previous;
 printf("enter the element you want to deleted:\n");
 scanf("%d",&a);
 for(current=list,previous=NULL;current!=NULL && (current->value)!=a;previous=current,current=current->next);
 if(current==NULL)
   {printf("The element you want to delete is not in the list\n");
       return list;
   }
 else if(previous==NULL)
   {list=list->next;
    return list;
   } 
 else
   {previous->next=current->next;
   free(current);
   return list;
}
}
struct node *append(struct node* list)/*appending the element at the end of the list*/

{int a;
printf("Enter the value of the element to be appended:");
scanf("%d",&a);
struct node* b=list;
struct node *temp1;
temp1=malloc(sizeof(struct node));
temp1->value=a;
temp1->next=NULL;
while (list->next!=NULL)
 {list=list->next;}
 list->next=temp1; 
printf("the new list is:\n");
while(b!=NULL)
{
printf("%d\n", b->value);
b=b->next;
}
}

struct node *extend(struct node *list)/*adding the new list to the previous list*/
{
   struct node *s, *temp1;
    temp1=list;
     int a,b,i;
    printf("Enter the number of elements you want to be there in the new list\n");
     scanf("%d",&a);

 while(temp1->next!=NULL)
{
temp1=temp1->next;
}
for(i=0;i<a;i++)
{
  printf("Enter the element\n");
   scanf("%d",&b);
   s=malloc(sizeof(struct node));
   s->value=b;
    s->next=NULL;
   temp1->next=s;
temp1=temp1->next;
}
return list;
}

struct node* insert(struct node *list)/*inserting an element at the desired position*/
{
int a,b,j=0;
printf("enter the element you want to insert\n");
  scanf("%d",&a);
 printf("enter the position at which you want to insert\n");
scanf("%d",&b);
 struct node *new_node,*p;
 new_node=malloc(sizeof(struct node));
 p=list;
 if(b==0)
  {new_node->value=a;
   new_node->next=p;
   return new_node;}
 else
 {for(;;p=p->next)
 {if(j==(b-1))
  {new_node->value=a;
   new_node->next=p->next;
   p->next=new_node;
   break;
  }j++;}
  return list;
 }
}
  int pop( struct node *list)/*deleting the element from the desired position*/
{ int i,a,x1;
struct node *current,*previous;
 printf("enter position of the element to be deleted\n");
 scanf("%d",&a);
 if(a==0)
 { return list->value;}
 else
 {current=list;previous=NULL;
  for(i=1;i<=a;i++)
   {previous=current;
    current=current->next;
   }
   previous->next=current->next;
  x1=current->value;
   free(current);
  printf("The new list is:");
         while(list!=NULL)
             {
                 printf("%d", list->value);
                 list=list->next;
            }
  
return x1;
   }
}
struct node *sort(struct node *list)  /*function to sort the list*/
 {
 struct node *x1 = NULL;
 struct node *x2 = NULL; 
 struct node *x3 = NULL;
 struct node *x4 = NULL; 
 struct node *x5 = NULL; 


 while(x4 != list->next) {
 x3 = x1 = list;
 x2 = x1->next;
  while(x1 != x4) {
   if(x1->value > x2->value) {
    if(x1 == list) {
     x5 = x2 -> next;
     x2->next = x1;
     x1->next = x5;
     list = x2;
     x3 = x2;
    } else {
     x5 = x2->next;
     x2->next = x1;
     x1->next = x5;
     x3->next = x2;
     x3 = x2;
    }
   } else {
    x3 = x1;
    x1 = x1->next;
   }
   x2 = x1->next;
   if(x2 == x4)
    x4 = x1;
  }
 }return list;
}




void display(struct node *list)/*function to display the list*/
{ 
 printf("The list is:");
         while(list!=NULL)
             {
                 printf("%d\t", list->value);
                 list=list->next;
            }

}

void main()
{
int n,x,i,ch,val1,ans;
struct node *first=NULL;
struct node *temp,*val;
temp=malloc(sizeof(struct node));
printf("Enter the number of elements you want to be there in the list\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
  printf("Enter the element\n");
   scanf("%d",&x);
  first=create(first,x);
 //val=first;
}
 display(first);

do{
printf("\nENTER YOUR CHOICE\n");
printf("1:reverse of the list\n");
printf("2:index of the number\n");
printf("3:count the number of occurence\n");
printf("4:sort the list\n");
printf("5:delete an element from the array\n");
printf("6:append the list \n");
printf("7:extend the list\n");
printf("8:insert the element\n");
printf("9:pop the element\n");
scanf("%d",&ch);
switch (ch)
{
case 1:val= reverse(first); 
           display(val);
           break;
case 2: index1(first);
             break;
case 3: count(first);
            break;
case 4:  first=sort(first);
              display(first);
             break;
case 5: val=delete(first);
             display(val);
             break;
case 6: val=append(first);
           display(val);
            break;
case 7: val=extend(first);
             display(val);
            break;
case 8:val=insert(first);
           display(val); 
           break;
case 9: val1=pop(first);
          printf("\n the element that is deleted is:%d\n",val1);
          break;
default : printf("wrong choice\n");
}
printf("do you want to continue\n");
printf("Enter 0 for no and 1 for yes\n");
scanf("%d",&ans);
}while(ans==1);
}

















 
